
// Version 0.0 pre alpha
/*
Needed files
*/
/*
resource.AddFile( "materials/Rlevel/adv/logo.vtf" )
resource.AddFile( "materials/Rlevel/adv/logo.vtm" )
resource.AddFile( "materials/Rlevel/adv/Logo_trans.vtf" )
resource.AddFile( "materials/Rlevel/adv/Logo_trans.vtm" )
*/
if !sql.TableExists("rleveldb") then
	sql.Query("CREATE TABLE IF NOT EXISTS rleveldb (Name varchar(50), steamID varchar(50), userid varchar(50) ,level INTEGER, credit INTEGER, xp INTEGER)")
	Msg("Created SQL Database\n")
else
	Msg("Rlevel_DB was loaded\n")
end

include("Rlevel/Skills/rlevel_skill_db.lua")
include("Rlevel/Get.lua")
include("Rlevel/player.lua")
include("Rlevel/Database.lua")
--AddCSLuaFile("autorun/adv.lua")
--AddCSLuaFile("autorun/client/cl_hud.lua")

function PlayerInitialSpawn( pl )

local userid = SeriliseSteamId( pl )
	if( PlayerExists(userid) == false ) then
		sql.Query( "INSERT INTO rleveldb ('Name', 'steamID', 'userid', 'level', 'credit', 'xp') VALUES ( '"..pl:GetName().."', '"..pl:SteamID().."', '"..userid.."', '1', '1', '1' )" )
	elseif( PlayerExists(userid) == false and userid == "BOT" ) then
		local userid = pl:GetName()
		sql.Query( "INSERT INTO rleveldb ('Name', 'steamID', 'userid', 'level', 'credit', 'xp') VALUES ( '"..pl:GetName().."', '"..pl:SteamID().."', '"..userid.."', '1', '1', '1' )" )
		end
		if( PlayerExists(userid) == false and PlayerExists( pl:GetName() ) == false ) then
			//Msg("Warning PLAYER DONT EXISTS\n Create Player-. "..userid.."")
			pl:SetNWInt("level",1)
			pl:SetNWInt("xp",500)
			pl:SetNWInt("credits",0)
			pl:SetNWInt("LongJump",100)
		elseif( PlayerExists(userid) == true ) then
		//Msg("Yay Player exists "..userid.." \n")
			pl:SetNWInt("level",GetPlayerLevel( userid ))
			pl:SetNWInt("xp",GetPlayerXp( userid ))
			pl:SetNWInt("credits",GetPlayerCredits( userid ))
			pl:SetNWInt("rxp",CalcExpInc(pl:GetNWInt("level")))
		end
	UpdatePlayer( pl , userid )
	pl:ChatPrint("On this server runs RLevel by |Royal|")
	--umsg.Start("PopUp",pl) umsg.End()
end

hook.Add("PlayerInitialSpawn","Createvalues",PlayerInitialSpawn)

function PlayerDeath( Victim, Inflictor, Attacker )

PlayerKill(Victim, Inflictor, Attacker)
CalcLevelUp( Attacker )
Log( Attacker , Victim )

end

hook.Add("PlayerDeath","PDeath",PlayerDeath)

function PlayerDisconnected( pl )

if( pl:IsBot() ) then
	timer.Destroy(pl:GetName())
else
	timer.Destroy(SeriliseSteamId( pl ))
	end
end

hook.Add("PlayerDisconnected","playerdisconnected",PlayerDisconnected)

function PrintDB(arg)

Msg(sql.QueryValue("SELCT userid FROM rleveldb WHERE Name = '"..arg.."'"))

end




	//